package com.catmiw.funds.service;

import java.io.IOException;
import java.io.Reader;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.ibatis.session.SqlSession;

public class SessionFactory {
	private static String CONFIGXML = "com/catmiw/funds/sqlmap/Configuration.xml";
	private static SessionFactory sessionFactory=null;

	private SqlSessionFactory sqlMapper=null;
	
	private SessionFactory(){
	}
	
	public static SessionFactory getInstance(){
		if (sessionFactory==null){
			sessionFactory=new SessionFactory() ;
		}
		return sessionFactory;
	}
	
	public SqlSession getSession(){
		if (sqlMapper==null){
			
			Reader reader=null;
			try {
				reader = Resources.getResourceAsReader(CONFIGXML);
				sqlMapper = new SqlSessionFactoryBuilder().build(reader);
			} catch (IOException e) {
				e.printStackTrace();
			}
			
		}
		return sqlMapper.openSession();
	}
}
